<?php
/**
 * 《联系信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Contact extends InfoUtil {
	public $tbl = Constant::tbl_contact;

	public $info = array(
		'-' => '请选择',
		'1' => '客户信息',
		'2' => '供应商',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'info' => '分类',
	
		'name' => '名称',
		'tel' => '电话',
		'mobile' => '手机',
		'email' => '邮箱',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('info_option', $this->info);
	}

	function tab($controller, $request) {
		$data = array(					
			'便笺信息' => array('cc'=>'info.php?do=Note.find', 'js'=>'1'),
			'二维条码' => array('cc'=>'info.php?do=Qr.find', 'js'=>'1'),
			'名片信息' => array('cc'=>'customer.php?do=Potential.find', 'js'=>'1'),

			'客户信息' => array('cc'=>'contact.php?do=Customer.find', 'js'=>'1'),
			'供应商' => array('cc'=>'contact.php?do=Supplier.find', 'js'=>'1'),
			'联系信息' => array('cc'=>'info.php?do=Contact.find', 'js'=>'1'),
			'银行帐号' => array('cc'=>'finance.php?do=Bank.find', 'js'=>'1'),
				
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/contact/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$info_id_s = $this->_input($request['info_id_s']);
		$name_s = $request['name_s'];

		$tel_s = $request['tel_s'];
		$info_s = $request['info_s'];

		$where = " WHERE 1=1";

		if (!empty($info_id_s)) {
			$where .= " AND `info_id` = '" . $info_id_s . "'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR email LIKE '%" . $name_s . "%')";
		}

		if (!empty($tel_s)) {
			$where .= sprintf(" AND （tel LIKE '%" . $tel_s . "%' OR mobile LIKE '%" . $tel_s . "%')", $tel_s, $tel_s);
		}

		if (!empty($info_s) && $info_s <> '-') {
			$where .= " AND info = '" . $info_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['info_t'] = $this->info[$row['info']];
				$row['a'] = $this->_info_name($row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/contact/list');
		return true;
	}
	
	function ldif($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['a'] = $this->_info_name($row);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'info/contact/ldif');
		return true;
	}
	
	function _batch_insql($request) {		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		return $insql;
	}

	function _info_name($row) {
		switch($row['info']) {
			case 1:
				$tbl = Constant::tbl_customer;
				break;
			case 2:
				$tbl = Constant::tbl_supplier;
				break;
			case 3:
				$tbl = Constant::tbl_potential;
				break;
			default:
				$tbl = Constant::tbl_supplier;				
		}

		return $this->id($tbl, $row['info_id']);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/contact/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$info = $request['info'];
		$info_id = $request['info_id'];

		$name = $request['name'];

		$tel = $request['tel'];
		$mobile = $request['mobile'];
		$email = $request['email'];
		$im = $request['im'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(info, info_id, name, tel, mobile, email, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id, $name, $tel, $mobile, $email);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 12, 1));

		$controller->display($request, 'info/contact/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$info = $request['info'];
		$info_id = $request['info_id'];

		$name = $request['name'];

		$tel = $request['tel'];
		$fax = $request['fax'];
		
		$mobile = $request['mobile'];
		$dh = $request['dh'];
		
		$email = $request['email'];
		$address = addslashes($request['address']);
		
		$qq = $request['qq'];
		$skype = $request['skype'];
		
		$content = addslashes($request['content']);

		$orderby = $request['orderby'];
		$status = $request['status'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET info = '%s', info_id = '%s', name = '%s', tel = '%s', fax = '%s', mobile = '%s', dh = '%s'";
		$fmt .= ", email = '%s', address = '%s', qq = '%s', skype = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id, $name, $tel, $fax, $mobile, $dh, $email, $address, $qq, $skype, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);
		
		if (!empty($request['qr'])) {
			return $this->qr($controller, $request);
		}

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		if (!empty($request['view_qr'])) {
			$controller->display($request, 'info/contact/view_qr');
			return true;
		}

		$controller->display($request, 'info/contact/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['info_t'] = $this->info[$row['info']];
		$row['a'] = $this->_info_name($row);
		
		if ($row['info'] == '1') {
			$row['view'] = "Customer.view";
		} else {
			$row['view'] = "Supplier.view";
		}
		
		$row = $this->_view_t($row);

		return $row;
	}

	function qr($controller, $request) {
		require_once(APP_LIB . 'phpqrcode/qrlib.php');
		
		$id = $request['id'];
		$row = $this->_view($id);
			    
		$controller->assign_by_ref('result', $row);
		
		$t = $this->_tj_html($controller, $request, 'info/contact/vcard');
		$value = $t;
		// $value = urlencode($t);
		
		$errorCorrectionLevel = 'M';
		$matrixPointSize = 3;
		
		return QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$db->query($sql);
			
			$this->_delete_ldap($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function num($info, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1'";
		$sql = sprintf($fmt, $this->tbl, $info, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_zjm($controller, $request) {
		$this->pretreat($controller);

		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$q = $request['q'];
		$key = sprintf("%s_%s_%s", $this->zjm, $this->prefix, $q);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "select id, code, zone, rate, name, zjm, continent, country from %s where zjm like '%s'";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['continent_t'] = $this->continent[$row['continent']];

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		return $ret;
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
	
	function ajax_id($controller, $request) {
		$q = $request['q'];
		$data = $this->id($this->tbl, $q);;
		echo json_encode($data);
	}
	
	function _update($info, $info_id, $json) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return false;
		}
		
		$name = trim($json->name);
		$tel = trim($json->tel);
		$mobile = trim($json->mobile);
		$content = trim($json->content);		
		if (empty($name)) {
			return false;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' AND name = '%s'";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id, $name);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$key = $this->tbl . "_id_" . $row['id'];
			clean($key);
				
			$fmt = "UPDATE %s SET tel = '%s', mobile = '%s', content = concat(content, '%s'), modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $tel, $mobile, $content, $row['id']);
			// $db->query($sql);
		} else {
			$fmt = "INSERT INTO %s(info, info_id, name, tel, mobile, content, oper, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $info, $info_id, $name, $tel, $mobile, $content, $_SESSION['id']);
			$db->query($sql);
		}
		debug($sql);
	}
	
	function _ajax_linkman($info, $info_id) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return array();
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id);
		debug($sql);
		$result = $db->query($sql);
		$ret = array(array('id'=>'-', 'name'=>'请选择'));
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function _linkman_primary($info, $info_id) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id);
		// debug($sql);
		$result = $db->query($sql);
		
		return $db->fetch_array($result);
	}
	
	function _insert($info, $id, $contact) {
		$a = json_decode($contact);
		if (!is_array($a) || count($a) == 0) {
			return false;
		}	

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(info, info_id, name, job, tel, fax, mobile, dh, email, address, qq, skype, content, oper, createtime, modifytime) ";
		$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		while (list($k, $v) = each($a)) {
			if (!$this->_is_insert($v)) {
				continue;
			}
		
			$linkman = addslashes($v->linkman);
			
			$email = strtolower($v->email);
			$address = strtolower($v->address);
			
			$content = addslashes($v->content);
			$sql = sprintf($fmt, $this->tbl, $info, $id, $linkman, $v->job, $v->tel, $v->fax, $v->mobile, $v->dh, $email, $address, $v->qq, $v->skype, $content, $_SESSION['id']);
			debug($sql);
			$db->query($sql);			
			$info_id = $db->insert_id();
			
			$this->_update_ldap($id, $info_id);
		}
	}
	
	function _update_ldap($id, $info_id) {
		$ds = $this->_connect();
		
		if (!$ds) {
			return false;
		}
		
		$row = $this->_view($info_id);		
		$row['a'] = $this->_info_name($row);
		
		$this->_ldap($ds, $row);
		
		ldap_close($ds);
	}	
	
	function _update2($info, $id, $contact) {
		$a = json_decode($contact);
		if (!is_array($a) || count($a) == 0) {
			return false;
		}

		$count = 1;
		$db = newdb($this->dsn_w);
		while (list($k, $v) = each($a)) {
			$linkman = addslashes($v->linkman);
			$email = strtolower($v->email);
			$address = strtolower($v->address);
			
			$content = addslashes($v->content);
			if (empty($v->id)) {
				if (!$this->_is_insert($v)) {
					continue;
				}

				$fmt = "INSERT INTO %s(info, info_id, name, job, dept, tel, fax, mobile, dh, email, address, geo, qq, skype, content, orderby, oper, createtime, modifytime)";
				$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $info, $id, $linkman, $v->job, $v->dept, $v->tel, $v->fax, $v->mobile, $v->dh, $email, $address, $v->geo, $v->qq, $v->skype, $content, $count, $_SESSION['id']);
				debug($sql);
				$db->query($sql);
				$info_id = $db->insert_id();
			} else {
				$key = $this->tbl . "_id_" . $v->id;
				clean($key);
				
				$fmt = "UPDATE %s SET name = '%s', job = '%s', dept = '%s', tel = '%s', fax = '%s', mobile = '%s', dh = '%s', email = '%s', address = '%s'";
				$fmt .= ", geo = '%s', qq = '%s', skype = '%s', content = '%s', orderby = '%s', modifytime = now() WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $linkman, $v->job, $v->dept, $v->tel, $v->fax, $v->mobile, $v->dh, $email, $address, $v->geo, $v->qq, $v->skype, $content, $count, $v->id);
				$info_id = $v->id;
				debug($sql);
				$db->query($sql);
			}

			$count += 3;
			
			$this->_update_ldap($id, $info_id);
		}
	}
	
	function _is_insert($v) {
		if (!empty($v->linkman)) {
			return true;
		}

		if (!empty($v->tel)) {
			return true;
		}
		
		if (!empty($v->fax)) {
			return true;
		}

		if (!empty($v->mobile)) {
			return true;
		}

		if (!empty($v->email)) {
			return true;
		}
		
		if (!empty($v->content)) {
			return true;
		}

		return false;
	}
	
	function _delete($z) {
		$a = json_decode($z);
		if (!is_array($a) || count($a) == 0) {
			return false;
		}
				
		$db = newdb($this->dsn_w);
		// $fmt = "DELETE FROM %s WHERE id = '%s'";
		$fmt = "UPDATE %s SET status = '3' WHERE id = '%s'";
		while (list($k, $id) = each($a)) {
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$db->query($sql);
			
			$this->_delete_ldap($id);
		}
	}
	
	function _delete_ldap($id) {
		$ds = $this->_connect();
		
		if (!$ds) {
			return false;
		}
		
		$user = sprintf("uid=%s,ou=supplier,dc=tecmold,dc=com", $id);
	    // echo $user . "\n";
	    
	    $r = ldap_list($ds, $user, "ou=*");
	    if ($r) {
	    	$r = ldap_delete($ds, $user);
	    	// debug("add");
	    }
	    
	    ldap_close($ds);
	}
	
	function _connect() {
		
		if (!function_exists('ldap_connect')) {
			return false;
		}
		
		$host = "172.16.1.128";
		$host = "ldap.tecmold.net";
		$port = 389;

		$ds = ldap_connect($host, $port) or die("Could not connect to {$host}");
		if ($ds) {
		    ldap_bind($ds, "cn=root,dc=tecmold,dc=com", "jeaf");
		}
		
		return $ds;
	}
	
	function ldap($controller, $request) {
		$ds = $this->_connect();		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s limit 5";
		$fmt = "SELECT * FROM %s WHERE info = '2' AND status = '1'";
		$fmt = "SELECT * FROM %s WHERE info = '2' AND modifytime > DATE_ADD(now(), INTERVAL -7 DAY)";
		// $fmt = "SELECT * FROM %s WHERE info = '2' and id = 1939";
		// $fmt = "SELECT * FROM %s WHERE info = '2'";
		
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (empty($row['name'])) {
				continue;
			}
			
			$info = array();
			
			$info["uid"] = $row['id'];		
		    $info["cn"] = $row['name'];
		    $info["sn"] = $row['name'];
			
			$info = $this->_ldap($info, $row);
			
			$info["objectclass"] = "inetOrgPerson";
	
		    $user = sprintf("uid=%s,ou=supplier,dc=tecmold,dc=com", $row['id']);
		    echo $user . "\n";
		    
		    $r = ldap_list($ds, $user, "ou=*");
		    print_r($info);
		    if (!$r) {
		    	$r = ldap_add($ds, $user, $info);
		    	echo ".";
		    } else {
		    	$r = ldap_modify($ds, $user, $info);
		    	echo "U";
		    }
		    // echo ldap_error($ds);
		}
		
		ldap_close($ds);

		logdate($controller, $request);
		return true;
	}
	
	function _ldap($info, $row) {
		$row['a'] = $this->_info_name($row);
			    
	    if (!empty($row['tel'])) {
	    	$info["telephonenumber"] = $row['tel'];
	    }
	    
	    if (!empty($row['fax'])) {
	    	$info["facsimiletelephonenumber"] = $row['fax'];
	    }
	    
	    if (!empty($row['mobile'])) {
	    	$info["mobile"] = $row['mobile'];
	    }
	    
	    if (!empty($row['email'])) {
	    	$info["mail"] = $row['email'];
	    }
	    
	    if (!empty($row['a']['address'])) {
	    	$info["street"] = $row['a']['address'];
	    }
	    
	    if (!empty($row['a']['fullname'])) {
	    	$info["o"] = $row['a']['fullname'];
	    }
	    
	   	return $info;
	}
	
	function ajax_count($controller, $request) {
		$email = trim($request['email']);
		$domain = strstr($email, '@');
		
		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$aa = new Customer();

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '1' AND email LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl,  "%" . $domain);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		$a = 0;
		$b = 0;
		while ($row = $db->fetch_array($result)) {
			$c = $aa->_view($row['info_id']);
			debug(print_r($c, true));
			if ($c['star'] > 0) {
				$a++;
			} else {
				$b++;
			}
			
			debug(print_r($row, true));
			
			$ret[] = $row;
		}
				
		if ($a > 0 || $b > 0) {
			$t = $a + $b;
			$ret = sprintf("此域名%s下有%s个电子邮件地址，PO客户%s个, RFQ客户%s个", $domain, $t, $a, $b);
		} else {
			$ret = " ";
		}

		echo $ret;
	}

	function _imperfect($info, $info_id) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id);
		debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($this->_is_perfect($row)) {
				return false;
			}
		}
		
		return true;
	}
	
	function _is_perfect($row) {
		if (empty($row['name'])) {
			return false;
		}

		if (!empty($row['tel'])) {
			return true;
		}
		
		if (!empty($row['fax'])) {
			return true;
		}

		if (!empty($row['mobile'])) {
			return true;
		}

		if (!empty($row['email'])) {
			return true;
		}
		
		if (!empty($row['content'])) {
			return true;
		}

		return false;
	}
	
	function merge($info, $from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET info_id = '%s' WHERE info = '%s' AND info_id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $info, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function view_customer($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_ajax_linkman('1', $id);
		
		$ret = $this->_shift($ret);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'info/contact/_list');
		return true;
	}
	
	function view_supplier($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_ajax_linkman('2', $id);
	
		$ret = $this->_shift($ret);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'info/contact/_list');
		return true;
	}
}
?>